package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtil {
	private static final String className="oracle.jdbc.OracleDriver";
	private static final String url="jdbc:oracle:thin:@zha-ita032-w7:1521:xe";
	private static final String userName = "jason";
	private static final String pwd = "233";
	private static BasicDataSource basicDataSource;	
	
	static{
		basicDataSource = new BasicDataSource();
		
		basicDataSource.setDriverClassName(className);
		basicDataSource.setUrl(url);
		basicDataSource.setUsername(userName);
		basicDataSource.setPassword(pwd);
		basicDataSource.setMaxActive(64);
	}
	/**
	 * create the connection
	 * @return connection
	 */
	public static Connection createConnection() {
		Connection connection = null;
		try {
			connection = basicDataSource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
	/**
	 * close
	 * @param connection
	 * @param pst
	 * @param rs
	 */
	public static void close(Connection connection,PreparedStatement pst, ResultSet rs){
		try {
			if(rs!=null)
				rs.close();
			if(pst!=null)
				pst.close();
			if(connection!=null)
				connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
